//
//  GXAuthorViewController.swift
//  BallGame
//
//  Created by GuangXiao on 16/3/19.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SDWebImage

//// 图片间间隙
//#define imageMargin 4
let imageMargin:CGFloat = 4
//// 总行数
let rows = 5
//#define rows 5
let cols = 3
//let imageCount = 15
//// 图片总数
//#define imageCount 15
class GXAuthorViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        setupUI()
    
    }

    func setupUI(){
        let scrollView = UIScrollView(frame: ScreenSize)
        
        //最后一行的imageView 的 y值 加上 图片的高度
        scrollView.contentSize = CGSizeMake(0, imageMargin + StatusHeight + CGFloat(pic_urls.count / cols) * ((ScreenHeight - imageMargin * CGFloat(rows+1))/CGFloat(rows) + imageMargin))
        
        view.addSubview(scrollView)
        // 图片的 宽W/高H
        let imageW = (ScreenWidth - imageMargin * CGFloat(cols+1))/CGFloat(cols)
        
        let imageH = (ScreenHeight - imageMargin * CGFloat(rows+1))/CGFloat(rows)
//        let imageCount = pic_urls.count
        for i in 0..<pic_urls.count{
            //图片所在的行
            let row = i/cols
            //图片所在的列
            let col = i%cols
            
            // 图片的X
            let imageX = imageMargin + CGFloat(col) * (imageW + imageMargin)
            // 图片的Y
            let imageY = imageMargin + StatusHeight + CGFloat(row) * (imageH + imageMargin)
            // 绘制 imageView
            let imageView = UIImageView(frame: CGRectMake(imageX, imageY, imageW, imageH))
            
            imageView.backgroundColor = UIColor(red: CGFloat(arc4random() % 256) / 255, green: CGFloat(arc4random() % 256) / 255, blue: CGFloat(arc4random() % 256) / 255, alpha: 1)
            
            // 创建进度条
            let progress = UIProgressView(frame: CGRectMake(0, 0, imageW, 2))

            // 进度条的颜色
            progress.tintColor = UIColor.redColor();
            
            // 将进度条添加到对应的图片上

            imageView.addSubview(progress)
            
            // 按顺序下载图片  异步 + 串行

            let urlString = pic_urls[i]
            let url = NSURL(string: urlString as! String)
            

            imageView.sd_setImageWithURL(url, placeholderImage: nil, options: .ProgressiveDownload, progress: { (receivedSize, expectedSize) -> Void in
                // 监听图片下载进度
                
                // 设置进度条进度
                // progress :当前的进度,取值范围: 0~1.0;
                // receivedSize : 当前已经下载的数据量
                // expectedSize :需要下载的总数据量
                progress.progress = Float(receivedSize)/Float(expectedSize);

                }, completed: { (_, _, _, _) -> Void in
                    print("下载完毕")
            })
            
            // 将 imageView 添加到控制器 View 上
            scrollView.addSubview(imageView)

        }
        

        
    }
    override func didReceiveMemoryWarning() {
        
        super.didReceiveMemoryWarning()
        
    }
    
    lazy var pic_urls:NSArray = {
        
        let path = NSBundle.mainBundle().pathForResource("photo.plist", ofType: nil)
        let pic_urls = NSArray(contentsOfFile: path!)
        print(pic_urls)
        return pic_urls!
    }()
}
